﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SharpTrainer.Util
{
    public class SerializeUtil
    {
        public static MemoryStream SerializeToMemory(Object objectGraph)
        {
            // Construct a stream that is to hold the serialized objects
            MemoryStream stream = new MemoryStream();

            // Construct a serialization formatter that does all the hard work
            BinaryFormatter formatter = new BinaryFormatter();

            // Tell the formatter to serialize the objects into the stream
            formatter.Serialize(stream, objectGraph);

            // Return the stream of serialized objects back to the caller
            return stream;
        }

        public static Object DeserializeFromMemory(Stream stream)
        {
            // Construct a serialization formatter that does all the hard work
            BinaryFormatter formatter = new BinaryFormatter();

            // Tell the formatter to deserialize the objects from the stream
            return formatter.Deserialize(stream);
        }
    }
}
